package com.ht.base.system.biz.manager.impl;

import com.ht.base.system.biz.convert.OnOffConfBeanUtil;
import com.ht.base.system.biz.manager.OnOffConfManager;
import com.ht.base.system.common.pojo.bo.OnOffConfBO;
import com.ht.base.system.common.pojo.qto.OnOffConfQTO;
import com.ht.base.system.dao.dao.OnOffConfDao;
import com.ht.base.system.dao.domain.OnOffConfDO;
import com.ht.middle.client.RedisClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.base.common.appexception.AppErrorCodeEnum;
import org.base.common.appexception.AppException;
import org.base.common.constans.StatusConstant;
import org.base.common.page.PageBO;
import org.base.common.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 系统开关配置Service
 * 业务层处理
 * @author hdengxian
 * @date 2024-03-28
 */
@Component
public class OnOffConfManagerImpl implements OnOffConfManager {
    Logger logger = LoggerFactory.getLogger(OnOffConfManagerImpl.class);

    private final String ON_OFF_PREFIX = "on_off_";
    @Resource
    private OnOffConfDao onOffConfDao;
    @DubboReference
    private RedisClient redisClient;
//    @Override
//    public void initToRedis() {
//        List<OnOffConfDO> condition = onOffConfDao.findByCondition(null);
//        if (CollectionUtils.isEmpty(condition)) {
//            return;
//        }
//        for (OnOffConfDO conf : condition) {
//            redisClient.set(ON_OFF_PREFIX + conf.getConfMark(), conf.getOpenStatus());
//        }
//    }

    /**
     * 新增
     * 系统开关配置
     * @param onOffConfBO
     * @return 结果
     */
    @Override
    public Integer insertData(OnOffConfBO onOffConfBO){
        if (onOffConfBO ==null) {
            throw new AppException("参数为空");
        }
        onOffConfBO.setCreateTime(new Date());
        OnOffConfDO saveData = OnOffConfBeanUtil.convertToDO(onOffConfBO);
        saveData.setCreateTime(new Date());
        saveData.setUpdateTime(new Date());
        int count = onOffConfDao.insert(saveData);
        redisClient.set(ON_OFF_PREFIX + saveData.getConfMark(), saveData.getOpenStatus());
        String logFlag = redisClient.get(ON_OFF_PREFIX + "logger_info_flag");
        if (StringUtils.isBlank(logFlag) || logFlag.equals(StatusConstant.STRING_1)) {
            logger.info("OnOffConfManagerImpl.insertData - Redis缓存数据已更新 - key={}", ON_OFF_PREFIX + saveData.getConfMark());
        }
        return count;
    }

    /**
     * 修改
     * 系统开关配置
     * @param onOffConfBO
     * @return 结果
     */
    @Override
    public Boolean updateDataById(OnOffConfBO onOffConfBO) {
        if (onOffConfBO == null || onOffConfBO.getOnOffConfId() == null){
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        JsonUtil.parseString("", "platSeqNo");
        OnOffConfDO updateData = OnOffConfBeanUtil.convertToDO(onOffConfBO);
        onOffConfDao.updateById(updateData);

        OnOffConfDO data = onOffConfDao.findById(onOffConfBO.getOnOffConfId());
        redisClient.set(ON_OFF_PREFIX + data.getConfMark(), data.getOpenStatus());

        return Boolean.TRUE;
    }

    /**
     * 批量删除
     * 系统开关配置
     * @param id
     * @return 结果
     */
    @Override
    public Boolean deleteById(Long id) {
        if (id == null){
            return Boolean.FALSE;
        }
        OnOffConfDO data = onOffConfDao.findById(id);
        onOffConfDao.deleteById(id);

        redisClient.delete(ON_OFF_PREFIX + data.getConfMark());
        return Boolean.TRUE;
    }

    /**
     * 批量删除
     * 系统开关配置
     * @param ids
     * @return 结果
     */
    @Override
    public Boolean deleteByIds(Long[] ids){
        if (ids == null) {
            return false;
        }
        List<OnOffConfDO> data = onOffConfDao.findOnOffConfByIds(Arrays.asList(ids));

        Integer count = onOffConfDao.deleteByIds(ids);

        for (OnOffConfDO conf : data) {
            redisClient.delete(ON_OFF_PREFIX + conf.getConfMark());
        }
        return Boolean.TRUE;
    }

    /**
     * 查询
     * 系统开关配置
     * @param id
     * @return 系统开关配置
     */
    @Override
    public OnOffConfBO findById(Long id) {
        OnOffConfDO data = onOffConfDao.findById(id);
        return OnOffConfBeanUtil.convertToBO(data);
    }

    /**
     * 分页查询列表
     * 系统开关配置
     * @param qto
     * @return 系统开关配置
     */
    @Override
    public PageBO<OnOffConfBO> findDataForPage(OnOffConfQTO qto) {
        PageBO<OnOffConfBO> pageBO = new PageBO();
        if (qto == null) {
            return pageBO;
        }
        Integer count = onOffConfDao.findForPageCount(qto);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<OnOffConfDO> doList = onOffConfDao.findForPage(qto);
        pageBO.setData(OnOffConfBeanUtil.convertToBOList(doList), count);
        return pageBO;
    }


}
